package com.fxkj.cloud.drms.domain.commandSettingHistory.service;

import com.fxkj.cloud.drms.domain.commandSettingHistory.CommandSettingHistoryDomain;
import com.fxkj.cloud.drms.domain.commandSettingHistory.repository.CommandSettingHistoryDomainRepository;
import com.fxkj.common.exception.FxErrorCode;
import com.fxkj.common.exception.FxException;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

/**
 * <p>description: </p>
 *
 * @author ww
 * @date 2025-03-25 11:05:54
 */
@Service
public class CommandSettingHistoryDomainServiceImpl implements CommandSettingHistoryDomainService {

    private final CommandSettingHistoryDomainRepository commandSettingHistoryDomainRepository;

    public CommandSettingHistoryDomainServiceImpl(CommandSettingHistoryDomainRepository commandSettingHistoryDomainRepository) {
        this.commandSettingHistoryDomainRepository = commandSettingHistoryDomainRepository;
    }

    @Override
    public CommandSettingHistoryDomain save(CommandSettingHistoryDomain commandSettingHistoryDomain) {
        return commandSettingHistoryDomainRepository.save(commandSettingHistoryDomain);
    }

    @Override
    public List<CommandSettingHistoryDomain> saveAll(List<CommandSettingHistoryDomain> commandSettingHistoryDomains) {
        return commandSettingHistoryDomainRepository.saveAll(commandSettingHistoryDomains);
    }

    @Override
    public CommandSettingHistoryDomain findById(UUID id, UUID organizationId) {
        return commandSettingHistoryDomainRepository.findById(id, organizationId);
    }

    @Override
    public Page<CommandSettingHistoryDomain> page(Integer page, Integer size, UUID deviceId, Integer attributeType, Long startTime, Long endTime, String deviceName, String username, UUID organizationId) {
        return commandSettingHistoryDomainRepository.page(PageRequest.of(page, size), deviceId, attributeType, startTime, endTime, deviceName, username, organizationId);
    }

    @Override
    public List<CommandSettingHistoryDomain> list(UUID organizationId) {
        return commandSettingHistoryDomainRepository.list(organizationId);
    }

    @Override
    public void delete(List<UUID> ids, UUID organizationId) throws FxException {
        List<CommandSettingHistoryDomain> commandSettingHistoryDomains = commandSettingHistoryDomainRepository.listByIds(ids, organizationId);
        if (commandSettingHistoryDomains.isEmpty() || commandSettingHistoryDomains.size() != ids.size()) {
            throw new FxException("删除失败", FxErrorCode.GENERAL);
        }
        commandSettingHistoryDomainRepository.deleteAll(commandSettingHistoryDomains);
    }

    @Override
    public List<CommandSettingHistoryDomain> getLatestCommandSettingHistoryKv(UUID deviceId, List<String> attributeKeys, UUID organizationId) {
        return commandSettingHistoryDomainRepository.getLatestCommandSettingHistoryKv(deviceId, attributeKeys, organizationId);
    }

}
